package config

import (
	"io"
	"os"
	"path"

	"github.com/BurntSushi/toml"
)

type tomlConfig struct {
	Viewer   Viewer
	System   SystemConfig
	Database Database
}

type Viewer struct {
	Title       string
	Author      string
	Description string
	Logo        string
	Navigation  []string
	Indextext   []string
}

type SystemConfig struct {
	AppName     string
	Version     float32
	CurrentDir  string
	InferApiUrl string
}

type Database struct {
	MongodbUrl string
}

var Cfg *tomlConfig

var OutputWriter io.Writer
var LogFile os.File

func init() {
	Cfg = new(tomlConfig)
	Cfg.System.AppName = "ms-go-blog"
	Cfg.System.Version = 1.0
	currentDir, _ := os.Getwd()
	Cfg.System.CurrentDir = currentDir
	_, err := toml.DecodeFile(currentDir+"/config/config.toml", &Cfg)
	if err != nil {
		panic(err)
	}
	filepath := path.Join(Cfg.System.CurrentDir, "log.txt")
	LogFile, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	if err != nil {
		panic(err)
	}
	OutputWriter = io.MultiWriter(os.Stdout, LogFile)
}
